import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * 将一个名为test1.txt的文件压缩到一个名为Compressed.zip的zip文件中
 * 注意：直接替换压缩包中的内容，如果
 */
public class ZipFile {

    public static void main(String[] args) throws IOException {

        //输出压缩包
        FileOutputStream fos = new FileOutputStream("src/main/resources/compressed.zip");
        ZipOutputStream zipOut = new ZipOutputStream(fos);

        //被压缩文件
        File fileToZip = new File("src/main/resources/test.txt");
        FileInputStream fis = new FileInputStream(fileToZip);

        //向压缩包中添加文件
        ZipEntry zipEntry = new ZipEntry(fileToZip.getName());
        zipOut.putNextEntry(zipEntry);
        byte[] bytes = new byte[1024];
        int length;
        while((length = fis.read(bytes)) >= 0) {
            zipOut.write(bytes, 0, length);
        }
        zipOut.close();
        fis.close();
        fos.close();
    }

}
